﻿using Intex.Tools;
using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core.Events;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;

namespace WPFExcelReport
{

    public class YCAFSAV95Data : BaseFunction
    {
        public INTEXDBC INTEXDBC;
        public SETINTEXDB INTEXDB = new SETINTEXDB();
        public DataSet SetCAD;
        public DataSet SetExt
        {
            get
            {
                return Convert2Intex.SetExt;
            }
            set
            {
                Convert2Intex.SetExt = value;
            }
        }
        public Convert2Intex Convert2Intex;
        public YCAFSAV95Data()
            : base()
        {
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public YCAFSAV95Data(OracleConnection _OracleDB, IntexReptServBLClient _proxy, string userID = "        ")
            : base(_OracleDB, _proxy, userID)
        {
            INTEXDBC = new INTEXDBC(_OracleDB, _proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public YCAFSAV95Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            Convert2Intex = new Convert2Intex();
            Helpers.Helper.GetDate(ref date, ref year, ref month, ref day);
        }
        public DataSet LoadEnv()
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();
            DataRow dwData = INTEXDB.SDDAT.NewRow();
            dwData["SDSA"] = "0178";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0196";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0312";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            dwData["SDSA"] = "0649";
            SReturn.Merge(INTEXDBC.QuerySDDAT(dwData, 9999));
            
            dwData = INTEXDB.LIEFDAT.NewLIEFDATRow();
            dwData["LSMANDANT"] = "00";
            dwData["LSGRSCHL"] = "COM";
            SResult = INTEXDBC.QueryLIEFDAT(dwData, 3);
            foreach (DataRow rx in SResult.Tables[0].Rows)
            {
                rx["LSLIEFNA"] = Convert8859P1ToGB2312(rx["LSLIEFNA"].ToString());
                rx["LSNAME1"] = Convert8859P1ToGB2312(rx["LSNAME1"].ToString());
            }
            SReturn.Merge(SResult);
            dwData = INTEXDB.ICC_SECTION.NewRow();
            dwData["SDSA"] = "0247";
            SResult = INTEXDBC.QuerySDDAT(dwData, 3);
            SResult.Tables[0].TableName = "ICC_SECTION";
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["SDDATEN"] = Helpers.Helper.GetGB2312Value(row["SDDATEN"].ToString());
            }
            SReturn.Merge(SResult);
            return SReturn;
        }
        public DataSet BuildData(SETINTEXDB src,DataRow dwInput)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                string krs = dwInput["KRS"].ToString();
                string lfd = dwInput["LFD"].ToString();
                string filter = "KRS='" + krs + "' and LFD='" + lfd + "'";
                DataTable tb = src.ICC_ABPOS_MAT.Select(filter).CopyToDataTable();
                tb.TableName = "ICC_ABPOS_MAT";
                Return.Merge(tb);
                DataRow dwData = INTEXDB.ABKOPF.NewRow();
                dwData["ABKMANDANT"] = "00";
                dwData["ABKAUFKRS"] = krs;
                dwData["ABKAUFLFD"] = lfd;
                string art = dwInput["ABPART"].ToString();
                string var = dwInput["ABPVAR"].ToString();
                string color = "";
                string size = "";
                Result = INTEXDBC.QueryABKOPF(dwData);
                Return.Merge(Result);
                #region=======Order======
                foreach (DataRow row in Return.Tables["ABKOPF"].Rows)
                {
                    dwData = INTEXDB.ABPOS.NewRow();
                    dwData["ABPMANDANT"] = row["ABKMANDANT"];
                    dwData["ABPAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABPAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPOS(dwData, 99);
                    Return.Merge(Result);
                    dwData = INTEXDB.ABPR.NewRow();
                    dwData["ARMANDANT"] = row["ABKMANDANT"];
                    dwData["ARAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ARAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABPR(dwData, 4);
                    
                    Return.Merge(Result);
                    dwData = INTEXDB.ABFOLGE.NewRow();
                    dwData["ABFMANDANT"] = row["ABKMANDANT"];
                    dwData["ABFAUFKRS"] = row["ABKAUFKRS"];
                    dwData["ABFAUFLFD"] = row["ABKAUFLFD"];
                    Result = INTEXDBC.QueryABFOLGE(dwData, 2);
                    Return.Merge(Result);
              
                }
                #endregion
                Result = GetABAUFM(dwInput);
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet GetABAUFM(DataRow dwABPOS)
        {
            DataSet Result;
            DataSet Return = new DataSet(); 
            string TableName = "ICC_ABAUFM";
            string FKRS = dwABPOS["KRS"].ToString().Trim();
            string FLFD = dwABPOS["LFD"].ToString().Trim();
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "select distinct decode(mtype,'T','T','F',DECODE(fsszuschn,'J','A','N','B','C','C',' ','D')) AS AA, "
                    + " a.abaart,a.abafarbe,a.abagroe "
                    + " ,NVL(TRIM(B.EKPLSARTBEZ),TRIM(B.EKPLSART)||'-'||TRIM(B.EKPLSFB)) AS ARTBEZ, "
                    + " to_char(to_date(trim(b.ekpkntrnrl),'dd.mm.yy'),'yyyymmdd') as findat,  "
                    + " b.ekpwuterm,abaaufkrs,abaauflfd "
                    + " from icc_abaufm a,ekpos b "
                    + " where abavkaufkrs = '" + FKRS + "' and abavkauflfd = '" + FLFD + "' "
                    + " and a.abaaufkrs = b.ekpaufkrs(+) "
                    + " and a.abaauflfd = b.ekpauflfd(+)  "
                    + " and a.abaaufpos = b.ekpaufpos(+)  "
                    + " ORDER BY AA,abaart,abafarbe "; 

                Result = Connect2Proxy(orlReadCommand);
                if (Result.Tables.Count > 0)
                {
                    Result.Tables[0].TableName = TableName;
                    byte[] b;
                    string value;
                    foreach (DataRow dw in Result.Tables[TableName].Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw["ARTBEZ"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw["ARTBEZ"] = value;
                        value = dw["AA"].ToString().Trim();
                        if (value == "A")
                        {
                            dw["AA"] = "主面料";
                        }
                        else if (value == "B")
                        {
                            dw["AA"] = "配布";
                        }
                        else if (value == "C")
                        {
                            dw["AA"] = "衬布";
                        }
                        else if (value == "D")
                        {
                            dw["AA"] = "里布";
                        }
                        else if (value == "T")
                        {
                            dw["AA"] = "辅料";
                        }
                    }
                }
                
                Return.Merge(Result);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet CreateMatrix(SETINTEXDB src,DataRow dwInput,string photosrc)
        {

            DataSet Return = new DataSet();
            DataTable tbABPOS = src.ABPOS;
            string filter = "KRS='" + dwInput["KRS"].ToString() + "' AND LFD='" + dwInput["LFD"].ToString() + "'";
            DataTable tbSC = src.ICC_ABPOS_MAT.Select(filter).CopyToDataTable();
            tbSC.TableName = "ICC_ABPOS_MAT";
            
            foreach (DataRow row in tbABPOS.Rows)
            {
                string art = row["ABPART"].ToString();
                string color = row["ABPFARBE"].ToString();
                string size = row["ABPGROE"].ToString().Trim();
                double qty = System.Convert.ToDouble(row["ABPEALFM"].ToString()) * 0.01;
                /*decimal ptqty = System.Convert.ToDecimal(dwInput["TQTYP"].ToString());
                if (ptqty > 0)
                    return Return;*/
                bool added = false;
                bool newcolor = false;
                int yc = tbSC.Columns.Count;
                int y = 0;
                added = false;
                DataRow rmx = null;
                var query = from DataRow rm in tbSC.AsEnumerable()
                            where rm["ABPART"].ToString() == art
                            && rm["ABPFARBE"].ToString() == color
                            select rm;
                foreach (DataRow rm in query)
                {
                    rmx = rm;
                }
                if (rmx == null)
                {
                    rmx = tbSC.NewRow();
                    newcolor = true;
                }
                rmx["ABPSPGRD"] = row["ABPSPGRD"].ToString();
                rmx["ABPGESSTA"] = row["ABPGESSTA"].ToString();                
                rmx["PHOTOLINK"] = rmx["PHOTOLINK"].ToString().Trim().Replace("D:/Images", photosrc);

                string PROGRPD = rmx["PROGRPD"].ToString();
                if (PROGRPD.Trim() == "")
                {
                    rmx["PROGRPD"] = Helpers.Helper.Get0196V(src, rmx["PROGRP"].ToString()); 
                   
                }
                string ARTGRPD = rmx["ARTGRPD"].ToString();
                if (ARTGRPD.Trim() == "")
                {
                    rmx["ARTGRPD"] = Helpers.Helper.Get0178V(src, rmx["ARTGRP"].ToString());
                }
                string MATARTD = rmx["MATARTD"].ToString();
                if (MATARTD.Trim() == "")
                {
                    rmx["MATARTD"] = Helpers.Helper.Get0649V(src, rmx["MATART"].ToString()); 
                }
                string MATGRPD = rmx["MATGRPD"].ToString();
                if (MATGRPD.Trim() == "")
                {
                    rmx["MATGRPD"] = Helpers.Helper.Get0312V(src, rmx["MATGRP"].ToString());
                } 
    
            } 
          
            Return.Merge(tbSC);
            return Return;
        }
        public DataSet Refresh(DataRow dwInput,string sections)
        {
            DataSet SResult = new DataSet();
            DataSet STemp = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                INTEXDBC.InsertICC_ABPOS_MAT(null, 1, "");
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 19);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 20);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 21);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 22);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 23);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 24);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 25);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 8);
                INTEXDBC.UpdateICC_ABPOS_MAT(null, 9);
                DataRow dwData = INTEXDB.ICC_ABPOS_MAT.NewRow();
                dwData["ABEND"] = dwInput["ABEND"]; 
                dwData["ARTDES"] =  Helpers.Helper.ParseSectionStr(sections);
                dwData["KOLL"] = dwInput["KOLL"];
                SResult = INTEXDBC.QueryICC_ABPOS_MAT(dwData, 4);
                SReturn.Merge(SResult);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }

        public DataSet GetWDECK(SETINTEXDB src, DataRow dwInput, DataRow dwABCO)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                DataRow dwData;
                dwData = INTEXDB.WASTAMM.NewRow();
                dwData["WAMANDANT"] = "00";
                dwData["WAART"] = dwInput["ART"];
                dwData["WAVAR"] = dwInput["VAR"];
                Result = INTEXDBC.QueryWASTAMM(dwData, 2);
                Return.Merge(Result);
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAVKAUFKRS"] = dwABCO["KRS"];
                dwData["ABAVKAUFLFD"] = dwABCO["LFD"];
                dwData["ABAART"] = dwInput["ART"];
                dwData["ABAVAR"] = Left(dwInput["VAR"].ToString() + "   ", 3);
                dwData["ABAFARBE"] = dwInput["FARBE"];
                dwData["ABAGROE"] = "       ";
                Result = INTEXDBC.QueryEKPOS(dwData, 99);
                Return.Merge(Result);
                //FSSTAMM;FSFARBEN;FSSTUELI;FSGROE
                dwData = INTEXDB.WAFARBEN.NewRow();
                dwData["WAFMANDANT"] = "00";
                dwData["WAFART"] = dwInput["ART"];
                dwData["WAFVAR"] = dwInput["VAR"];
                Result = INTEXDBC.QueryWAFARBEN(dwData, 2);
                Return.Merge(Result);

                dwData = INTEXDB.WBBEW.NewRow();
                dwData["WBEWMANDANT"] = "00";
                dwData["WBEWAUFKRS"] = dwABCO["KRS"];
                dwData["WBEWAUFLFD"] = dwABCO["LFD"];
                Result = INTEXDBC.QueryWBBEW(dwData, 99);
                Return.Merge(Result);
                decimal pricestd = 0;
                decimal fglfd = 1;
                foreach (DataRow rtemp in Return.Tables["WASTAMM"].Rows)
                {
                    string um = rtemp["WAUEBM"].ToString().Trim();

                    string srcs = rtemp["WABIND"].ToString().Trim();

                    if (srcs.Trim() == "")
                    {
                        srcs = "0";
                        var query = from DataRow rwc in Return.Tables["WAFARBEN"].AsEnumerable()
                                    where rwc["WAFART"].ToString().Trim() == dwInput["ART"].ToString().Trim()
                                    && rwc["WAFZUSATZ"].ToString().Trim() == dwInput["FARBE"].ToString().Trim()
                                    select rwc;
                        foreach (DataRow rwc in query)
                        {
                            srcs = (System.Convert.ToDecimal(rwc["WAFZUABAK"].ToString()) / 100).ToString();
                        }
                    }
                    pricestd = System.Convert.ToDecimal(srcs);
                }
                foreach (DataRow row in Return.Tables["WBBEW"].Rows)
                {
                    SetActualData(ref src, row);
                }
                foreach (DataRow row in Return.Tables["EKPOS"].Rows)
                {
                    SetActualData(ref src, row, pricestd);
                }
                foreach (DataRow row in src.WDECK.Rows)
                {
                    SetActualData(ref src, row);
                }
                Return.Merge(src.ICC_XDECK);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public DataSet GetFDECK(SETINTEXDB src, DataRow dwInput, DataRow dwABCO)
        {
            DataSet Result = new DataSet();
            DataSet Return = new DataSet();
            try
            {
                string art = "", var = "", color = "", size = "";
                DataRow dwData;
                art = dwInput["ART"].ToString();
                var = dwInput["VAR"].ToString();
                color = dwInput["FARBE"].ToString();
                size = dwInput["SIZE"].ToString();

                dwData = INTEXDB.FSSTAMM.NewRow();
                dwData["FSMANDANT"] = "00";
                dwData["FSART"] = dwInput["ART"];
                dwData["FSVAR"] = Left(var, 2);
                Result = INTEXDBC.QueryFSSTAMM(dwData, 1);
                Return.Merge(Result);
                dwData = INTEXDB.ICC_ABAUFM.NewRow();
                dwData["ABAVKAUFKRS"] = dwABCO["KRS"];
                dwData["ABAVKAUFLFD"] = dwABCO["LFD"];
                dwData["ABAART"] = art;
                dwData["ABAVAR"] = Left(dwInput["VAR"].ToString() + "   ", 3);
                dwData["ABAFARBE"] = color;
                dwData["ABAGROE"] = size;
                Result = INTEXDBC.QueryEKPOS(dwData, 99);
                Return.Merge(Result);
                DataRow rms = INTEXDB.FSEINK.NewFSEINKRow();
                rms["FEMANDANT"] = "00";
                rms["FEART"] = art;
                rms["FEVAR"] = Left(var, 2);
                Result = INTEXDBC.QueryFSEINK(rms, 2);
                Return.Merge(Result);
                /*dwData = INTEXDB.FSFARBEN.NewRow();
                dwData["FSFMANDANT"] = "00";
                dwData["FSFART"] = dwInput["ART"];
                dwData["FSFVAR"] = dwInput["VAR"];
                Result = INTEXDBC.QueryFSFARBEN(dwData, 2);
                Return.Merge(Result);
                dwData = INTEXDB.FSSTUELI.NewRow();
                dwData["FSSMANDANT"] = "00";
                dwData["FSSART"] = dwInput["ART"];
                Result = INTEXDBC.QueryFSSTUELI(dwData, 2);
                Return.Merge(Result);*/
                dwData = INTEXDB.FBEW.NewRow();
                dwData["FBWMANDANT"] = "00";
                dwData["FBWAUFKRS"] = dwABCO["KRS"];
                dwData["FBWAUFLFD"] = dwABCO["LFD"];
                Result = INTEXDBC.QueryFBEW(dwData, 99);
                Return.Merge(Result);
                decimal pricestd = 0;
                string filter = "";
                DataRow[] rows;
                DataRow rprice = null;
                //order by FEART DESC, FEFARBE DESC,FEGROE DESC
                filter = "FEFARBE='" + Left(color, 10) + "' AND FEGROE='" + Left(size, 7) + "'";
                rows = Return.Tables["FSEINK"].Select(filter);
                if (rows.Length > 0)
                {
                    rprice = rows[0];
                }
                if (rprice == null)
                {
                    if (color.Trim() != "")
                    {

                        filter = "FEFARBE='" + Left(color, 10) + "'";
                        rows = Return.Tables["FSEINK"].Select(filter);
                        if (rows.Length > 0)
                        {
                            rprice = rows[0];
                        }
                    }
                }
                if (rprice == null)
                {
                    if (size.Trim() != "")
                    {
                        filter = "FEGROE='" + Left(size, 7) + "'";
                        rows = Return.Tables["FSEINK"].Select(filter);
                        if (rows.Length > 0)
                        {
                            rprice = rows[0];
                        }
                    }
                }
                //FSUEBM

                if (rprice == null)
                {
                    rows = Return.Tables["FSEINK"].Select();
                    if (rows.Length > 0)
                    {
                        rprice = rows[0];
                    }
                }
                if (rprice != null)
                    pricestd = System.Convert.ToDecimal(rprice["FEPREIS1"].ToString()) / 100;
                foreach (DataRow row in Return.Tables["FBEW"].Rows)
                {
                    SetActualData(ref src, row);
                }
                foreach (DataRow row in Return.Tables["EKPOS"].Rows)
                {
                    SetActualData(ref src, row, pricestd);
                }
                foreach (DataRow row in src.FDECK.Rows)
                {
                    SetActualData(ref src, row);
                }
                Return.Merge(src.ICC_XDECK);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return Return;
        }
        public void SetActualData(ref SETINTEXDB src, DataRow dwInput, decimal pricestd = 0)
        {
            string tbname = dwInput.Table.TableName;
            string krs = "", lfd = "", pos = "", art = "", var = "", farbe = "", size = "", qty = "0";
            decimal price = 0;
            string um = "";
            decimal fglfd = 1;

            switch (tbname)
            {
                case "WBBEW":
                    //WBEWAUFKRS,WBEWAUFLFD,WBEWART,WBEWVAR,WBEWFARBE,WBEWBESCHICH,WBEWBS,SUM(WBEWMTR*0.001) AS WBEWMTR
                    krs = dwInput["WBEWAUFKRS"].ToString();
                    lfd = dwInput["WBEWAUFLFD"].ToString();
                    pos = dwInput["WBEWAUFPOS"].ToString();
                    art = dwInput["WBEWART"].ToString();
                    var = dwInput["WBEWVAR"].ToString();
                    farbe = dwInput["WBEWFARBE"].ToString();
                    size = "       ";
                    qty = dwInput["WBEWMTR"].ToString();
                    break;
                case "FBEW":
                    //FBWAUFKRS,FBWAUFLFD,FBWAUFPOS,FBWART,FBWVAR,FBWFARBE,FBWGROE,FBWBS,SUM(FBWME) AS FBWME
                    krs = dwInput["FBWAUFKRS"].ToString();
                    lfd = dwInput["FBWAUFLFD"].ToString();
                    pos = dwInput["FBWAUFPOS"].ToString();
                    art = dwInput["FBWART"].ToString();
                    var = dwInput["FBWVAR"].ToString();
                    farbe = dwInput["FBWFARBE"].ToString();
                    size = dwInput["FBWGROE"].ToString();
                    qty = dwInput["FBWME"].ToString();
                    break;
                case "EKPOS":
                    //ABAVKAUFKRS,ABAVKAUFLFD,MU.ABAART,MU.ABAVAR,MU.ABAFARBE,ABAGROE,EK.EKPPRME*0.001 AS PRICE,EKPPREH AS UM
                    krs = dwInput["ABAVKAUFKRS"].ToString();
                    lfd = dwInput["ABAVKAUFLFD"].ToString();
                    pos = dwInput["ABAVKAUFPOS"].ToString();
                    art = dwInput["ABAART"].ToString();
                    var = dwInput["ABAVAR"].ToString();
                    farbe = dwInput["ABAFARBE"].ToString();
                    size = dwInput["ABAGROE"].ToString();
                    price = System.Convert.ToDecimal(dwInput["PRICE"].ToString());
                    um = dwInput["UM"].ToString().Trim();
                    fglfd = System.Convert.ToDecimal(dwInput["WAFGRLFD"].ToString());
                    break;
                case "WDECK":
                    krs = dwInput["WDAUFKRS"].ToString();
                    lfd = dwInput["WDAUFLFD"].ToString();
                    pos = dwInput["WDAUFPOS"].ToString();
                    art = dwInput["WDART"].ToString();
                    var = dwInput["WDVAR"].ToString();
                    farbe = dwInput["WDFARBE"].ToString();
                    size = "       ";
                    qty = dwInput["WDBEDARF"].ToString();
                    break;
                case "FDECK":
                    krs = dwInput["FDAUFKRS"].ToString();
                    lfd = dwInput["FDAUFLFD"].ToString();
                    pos = dwInput["FDAUFPOS"].ToString();
                    art = dwInput["FDART"].ToString();
                    var = dwInput["FDVAR"].ToString();
                    farbe = dwInput["FDFARBE"].ToString();
                    size = dwInput["FDGROE"].ToString();
                    qty = dwInput["FDMENGE"].ToString();
                    break;
            }
            var query = from DataRow row in src.ICC_XDECK.AsEnumerable()
                        where row["KRS"].ToString() == krs
                        && row["LFD"].ToString() == lfd
                        && row["POS"].ToString() == pos
                        && row["ART"].ToString() == art
                        && row["VAR"].ToString().Trim() == var.Trim()
                        && row["COLOR"].ToString() == farbe
                        && row["ASIZE"].ToString() == size
                        select row;
            foreach (DataRow row in query)
            {
                if (System.Convert.ToDouble(row["CONTROL"].ToString().Trim()) > 89)
                    continue;
                switch (tbname)
                {
                    case "WDECK":
                        row["ARTDES"] = dwInput["ARTDES"];
                        row["CDES"] = dwInput["CDES"];
                        row["QTYPLAN"] = System.Convert.ToDouble(qty);
                        break;
                    case "FDECK":
                        row["ARTDES"] = dwInput["ARTDES"];
                        row["CDES"] = dwInput["CDES"];
                        row["QTYPLAN"] = System.Convert.ToDouble(qty) * 100;
                        break;
                    case "WBBEW":
                    case "FBEW"://如果已经审核，则不可以改变
                        row["QTYACT"] = System.Convert.ToDouble(qty) * 0.01;
                        break;
                    case "EKPOS":
                        if (um.Trim() == "KG")
                        {
                            price = System.Math.Round(price * fglfd / 1000, 3);
                            pricestd = System.Math.Round(pricestd * fglfd / 1000, 3);
                        }
                        row["PREK"] = price;
                        row["PRSTD"] = pricestd;
                        row["EKPAUF"] = dwInput["EKPAUFKRS"].ToString() + "-" + dwInput["EKPAUFLFD"].ToString() + "-" + dwInput["EKPAUFPOS"].ToString();
                        break;
                }
            }
        }
        public DataSet ResetData(SETINTEXDB src, DataRow dwABPOS, string photosrc)
        {
            DataSet SResult = new DataSet();
            DataSet SReturn = new DataSet();

            try
            {
                //按颜色的成本分析

                SReturn.Merge(CreateMatrix(src, dwABPOS, photosrc));
                SReturn.Merge(src.ABPOS);
                SReturn.Merge(ResetWDECK(src));
                SReturn.Merge(ResetFDECK(src));
                SReturn.Merge(src.ICC_XDECK);
                SReturn.Merge(src.WDECK);
                SReturn.Merge(src.FDECK);
                SReturn.Merge(src.ABKOPF);
                SReturn.Merge(src.ABFOLGE);
                SReturn.Merge(src.ABPR);
                SReturn.Merge(src.ICC_ABAUFM); 
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        private void ResetABCO(SETINTEXDB src, DataRow dwABCO)
        {
            if (dwABCO == null)
                return;
            string end = "Y";
            decimal enddate = 0;
            decimal qty = 0;
            foreach (DataRow rx in src.ABPOS.Rows)
            {
                qty += System.Convert.ToDecimal(rx["ABPEALFM"].ToString());
                if (rx["ABPERLKZ"].ToString() == " ")
                {
                    end = "N";
                }
                if (System.Convert.ToDecimal(rx["ABPERLDAT"].ToString()) > enddate)
                {
                    enddate = System.Convert.ToDecimal(rx["ABPERLDAT"].ToString());
                }
                //ABPEALFM,ABPERLKZ,ABPERLDAT
            }

            dwABCO["END"] = end;
            if (end == "Y")
            {
                dwABCO["ENDDATE"] = enddate;
            }
            dwABCO["QTY"] = qty;
            /*//获取加工单价
            var price = (from s in src.ABPR.AsEnumerable()
                         select System.Convert.ToDecimal(s["ARWERT"].ToString())
                          ).Max();
            dwABCO["MPRICE"] = price;*/
        }
        public DataSet ResetWDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {

                DataRow dwABPOS = null;
                DataTable tbFabric = INTEXDB.ICC_WDECKMAT.Clone();
                tbFabric.TableName = "ICC_WDECKMAT";
                foreach (DataRow rwd in src.WDECK.Rows)
                {
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;
                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where rx["ABPAUFKRS"].ToString() == rwd["WDAUFKRS"].ToString()
                                && rx["ABPAUFLFD"].ToString() == rwd["WDAUFLFD"].ToString()
                                && rx["ABPAUFPOS"].ToString() == rwd["WDAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(rwd["WDBEDARF"].ToString());
                    string mum = rwd["UM"].ToString().Trim();
                    string rlfd = rwd["FGRLFD"].ToString().Trim();
                    string mart = rwd["WDART"].ToString();
                    string mvar = rwd["WDVAR"].ToString();
                    string mcolor = rwd["WDFARBE"].ToString();

                    var qg = from DataRow rowt in tbFabric.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             && rowt["FARBE"].ToString() == mcolor
                             select rowt;

                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbFabric.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["FARBE"] = mcolor;
                    rt["UM"] = mum;
                    rt["FGRLFD"] = rwd["FGRLFD"];
                    rt["TQTY"] = lastqty + mqty;//暂时用STQTY代表实际数量
                    rt["STQTY"] = rt["TQTY"];
                    if (mum.Trim() == "KG")
                    {
                        //rt["STQTY"] = (lastqty + mqty) * System.Convert.ToDecimal(rlfd) / 1000;
                    }
                    if (newrow)
                    {
                        tbFabric.Rows.Add(rt);
                    }
                }
                SReturn.Merge(tbFabric);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        public DataSet ResetFDECK(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            double colortotal = 0;
            double total = 0;
            int yc = 0;
            try
            {

                DataRow dwABPOS = null;

                DataTable tbFMat = INTEXDB.ICC_FDECKMAT.Clone();
                tbFMat.TableName = "ICC_FDECKMAT";
                foreach (DataRow row in src.FDECK.Rows)
                {
                    if (row["FDTYP"].ToString() == "Z")
                        continue;
                    DataRow rt = null;
                    bool newrow = false;
                    decimal lastqty = 0;
                    dwABPOS = null;
                    var query = from DataRow rx in src.ABPOS.AsEnumerable()
                                where rx["ABPAUFKRS"].ToString() == row["FDAUFKRS"].ToString()
                                && rx["ABPAUFLFD"].ToString() == row["FDAUFLFD"].ToString()
                                && rx["ABPAUFPOS"].ToString() == row["FDAUFPOS"].ToString()
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        dwABPOS = rx;
                    }
                    if (dwABPOS == null)
                        continue;
                    decimal mqty = System.Convert.ToDecimal(row["FDMENGE"].ToString());
                    string mart = row["FDART"].ToString();
                    string mvar = row["FDVAR"].ToString();
                    string mcolor = row["FDFARBE"].ToString();
                    string msize = row["FDGROE"].ToString();
                    string mum = "PCS";
                    if (!tbFMat.Columns.Contains(msize))
                    {
                        tbFMat.Columns.Add(msize, typeof(System.Decimal));
                    }
                    var qg = from DataRow rowt in tbFMat.AsEnumerable()
                             where rowt["ART"].ToString() == mart
                             && rowt["VAR"].ToString() == mvar
                             && rowt["FARBE"].ToString() == mcolor
                             && rowt["SIZE"].ToString() == msize
                             select rowt;
                    foreach (DataRow rowt in qg)
                    {
                        rt = rowt;
                        if (rowt["TQTY"].ToString().Trim() == "")
                        {
                            rowt["TQTY"] = "0";
                        }
                        lastqty = System.Convert.ToDecimal(rowt["TQTY"].ToString());
                    }
                    if (rt == null)
                    {
                        newrow = true;
                        rt = tbFMat.NewRow();
                    }
                    rt["ART"] = mart;
                    rt["VAR"] = mvar;
                    rt["FARBE"] = mcolor;
                    rt["SIZE"] = msize;
                    rt["UM"] = mum;
                    rt["TQTY"] = lastqty + mqty;
                    if (newrow)
                    {
                        tbFMat.Rows.Add(rt);
                    }
                }
                SReturn.Merge(tbFMat);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }
        #region========OLD=================
        public DataSet ResetABPR(SETINTEXDB src)
        {
            DataSet Result = new DataSet();
            DataSet SReturn = new DataSet();
            try
            {
                /*ARAENDBEN ARAENDDAT*/
                foreach (DataRow row in src.ABPOS.Rows)
                {
                    string client = row["ABPMANDANT"].ToString();
                    string krs = row["ABPAUFKRS"].ToString();
                    string lfd = row["ABPAUFLFD"].ToString();
                    string pos = row["ABPAUFPOS"].ToString();
                    string user = row["ABPAENDBEN"].ToString();
                    string date = row["ABPAENDDAT"].ToString();
                    var query = from rx in src.ABPR.AsEnumerable()
                                where rx["ABAAUFKFRS"].ToString() == krs
                                && rx["ABAAUFKLFD"].ToString() == lfd
                                && rx["ABAAUFKPOS"].ToString() == pos
                                select rx;
                    foreach (DataRow rx in query)
                    {
                        rx["ARAENDBEN"] = user;
                        rx["ARAENDDAT"] = date;
                    }
                }
                SReturn.Merge(src.ABPR);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return SReturn;
        }        
        #endregion
    }
}
